package top.hcode.hoj.crawler.problem;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import top.hcode.hoj.pojo.entity.problem.Problem;
import top.hcode.hoj.utils.Constants;

import java.util.List;

/**
 * 洛谷题目
 *
 * @author orangej
 * @since 2024/11/13
 */
public class LuoguProblemStrategy extends ProblemStrategy {

    @Override
    public RemoteProblemInfo getProblemInfo(String problemId, String author) throws Exception {
        HttpResponse response = HttpRequest.get("https://www.luogu.com.cn/problem/" + problemId + "?_contentOnly=1")
                .header("Accept", "application/json")
                .header("User-Agent", "UniversalOJ/1.0 UOJ-Luogu-RemoteJudge/1.0 ( https://github.com/renbaoshuo/UOJ-Luogu-RemoteJudge )")
                .timeout(6000)
                .execute();
        if (!response.isOk()) {
            throw new RuntimeException("获取题目失败 http status:" + response.getStatus() + ", " + response.body());
        }

        String content = response.body();
        JSONObject result = JSONUtil.parseObj(content);
        JSONObject problem = result.getJSONObject("currentData").getJSONObject("problem");

        Problem info = new Problem();
        info.setProblemId(Constants.RemoteOJ.LUOGU.getLabel() + "-" + problemId);
        info.setTitle(problem.getStr("title"));
        JSONObject limits = problem.getJSONObject("limits");
        info.setTimeLimit(parseTimeLimit(limits));
        info.setMemoryLimit(parseMemoryLimit(limits));
        info.setDescription(problem.getStr("description"));
        info.setInput(problem.getStr("inputFormat"));
        info.setOutput(problem.getStr("outputFormat"));
        info.setExamples(parseExamples(problem.getJSONArray("samples")));
        info.setHint(problem.getStr("hint"));
        info.setIsRemote(true);
        info.setOj(Constants.RemoteOJ.LUOGU.name());
//        info.setSource();

        info.setType(0)
                .setAuth(1)
                .setAuthor(author)
                .setOpenCaseResult(false)
                .setIsRemoveEndBlank(false)
                .setIsGroup(false)
                .setDifficulty(1); // 默认为中等

        return new RemoteProblemInfo()
                .setProblem(info)
                .setTagList(null)
                .setRemoteOJ(Constants.RemoteOJ.LUOGU);
    }

    String parseExamples(JSONArray sampleArr) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < sampleArr.size(); i++) {
            JSONArray sample = sampleArr.getJSONArray(i);
            String input = sample.getStr(0);
            String output = sample.getStr(1);
            builder.append("<input>").append(input).append("</input>");
            builder.append("<output>").append(output).append("</output>");
        }
        return builder.toString();
    }

    int parseTimeLimit(JSONObject limits) {
        JSONArray timeArr = limits.getJSONArray("time");
        List<Integer> timeList = timeArr.toList(Integer.class);
        return CollUtil.max(timeList);
    }

    int parseMemoryLimit(JSONObject limits) {
        JSONArray memoryArr = limits.getJSONArray("memory");
        List<Integer> memoryList = memoryArr.toList(Integer.class);
        return CollUtil.max(memoryList);
    }


}
